require('../css/editInfo.less')
require('../css/common-css/basic.css')
require('../utils/common.js')
const baseURL = require('../utils/baseURL.js')


window.onload = function () {
    let i = document.querySelector('i')
    i.addEventListener('click', function () {
        location.href = 'mine.html'
    })
    // 定义一个全局变量
    var provinceid
    var genderid
    var cityid
    let sign = document.querySelector('#sign')
    let nickName = document.querySelector('#nickName')

    // 修改头像
    let file = document.querySelector('#file')
    let a = document.querySelectorAll('a')
    let userImg = document.querySelector('#userImg')
    a[0].addEventListener('click', function () {
        file.click()
        file.addEventListener('change', function () {
            let formdata = new FormData()
            formdata.append('file', file.files[0])
            axios.post(
                '/api/shared/uploadPortrait1', formdata
            ).then(function (res) {
                console.log(res.data.data.url);
                let imgs = {
                    imgUrl: res.data.data.url
                }
                console.log(userImg);
                axios.post(
                    '/api/user/changeInfo', imgs
                ).then(function (res) {
                    axios.get('/api/user/info').then(function (res) {
                        let data = res.data.data
                        console.log(data);
                        // 头像
                        userImg.src = baseURL + data.imgUrl
                    })
                })
            })
        })
    })
    // 获取性别，修改性别
    let gender = document.querySelector('#gender')
    let sex = document.querySelector('.sex')
    gender.addEventListener('click', function () {
        weui.picker([{
            label: '女',
            value: 0
        }, {
            label: '男',
            value: 1
        }], {
            onConfirm: function (result) {
                sex.innerHTML = result[0].label
                genderid = result[0].value
            },
        });
    })
    // 修改生日
    let birthday = document.querySelector('#birthday')
    let days = document.querySelector('.days')
    birthday.addEventListener('click', function () {
        weui.datePicker({
            start: 1900,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                let year = result[0].value
                let month = result[1].value
                let day = result[2].value
                let date = year + '-' + dateCommon(month) + '-' + dateCommon(day)
                days.innerHTML = date
            },
        });
    })
    // 获取省份，并进行修改
    let province = document.querySelector('.province')
    let pve = document.querySelector('#pve')
    axios.get('api/shared/province').then(function (res) {
        let provinces = res.data.data
        let newData = provinces.map((item) => {
            return {
                value: item.id,
                label: item.name
            }
        })
        pve.addEventListener('click', function () {
            weui.picker(newData, {
                onConfirm: function (result) {
                    province.innerHTML = result[0].label
                    provinceid = result[0].value
                }
            });
        })
    })
    // 获取城市
    let citys = document.querySelector('#citys')
    let city = document.querySelector('.city')
    // 判断是否选择了省份
    citys.addEventListener('click', function () {
        if (provinceid) {
            axios.get('/api/shared/city?provinceId=' + provinceid
            ).then(function (res) {
                let arr = res.data.data
                let newCitys = arr.map((item) => {
                    return {
                        value: item['id'],
                        label: item['name']
                    }
                })
                weui.picker(newCitys, {
                    onConfirm: function (result) {
                        city.innerHTML = result[0].label
                        cityid = result[0].value
                    }
                });
            })
        } else {
            common.toast('请选择省份', false)
        }
    })
    // 保存按钮
    let btn = document.querySelector('.btn')
    btn.addEventListener('click', function () {
        let params = {
            nickName: nickName.value,
            gender: genderid,
            provinceId: provinceid,
            cityId: cityid,
            birthday: days.innerText,
            sign: sign.value
        }
        axios.post('/api/user/changeInfo', params).then(function (res) {
            console.log(res);
            if (res.data.errno == 0) {
                common.toast('修改成功', true)
                setTimeout(function () {
                    location.href = 'mine.html'
                }, 2000)
            } else {
                common.toast('修改失败', false)
            }
        })
    })

    // 显示默认数据
    axios.get('/api/user/info').then(function (res) {
        let data = res.data.data
        console.log(data);
        // 头像
        userImg.src = baseURL + data.imgUrl

        // 用户名
        if (data.nickName) {
            nickName.value = data.nickName
        }

        // 性别
        if (data.gender == 1) {
            sex.innerHTML = '男'
        } else {
            sex.innerHTML = '女'
        }

        // 生日
        if (data.birthday) {
            days.innerHTML = data.birthday
        }

        // 省份
        axios.get('api/shared/province').then(function (res) {
            let provinces = res.data.data.find(function (item) {
                return item.id == data.provinceId
            })
            province.innerHTML = provinces.name
        })

        // 城市
        axios.get('/api/shared/city?provinceId=' + data.provinceId).then(function (res) {
            console.log(res);
            let cityText = res.data.data.find(function (item) {
                return item.id == data.cityId
            })
            city.innerHTML = cityText.name
        })
        // 签名
        if (data.sign) {
            sign.value = data.sign
        }
    })
}

// 封装月份和天数小于在前面添加一个0
function dateCommon(num) {
    if (num < 10) {
        num = '0' + num
        return num
    } else {
        return num
    }
}